﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ElevateThis
{
    /// <summary>
    /// Class to parse the command line
    /// </summary>
    public static class CommandLineParser
    {
        /// <summary>
        /// Parse command line options
        /// </summary>
        public static Options ParseCommandLine(string[] args)
        {
            var commandLineArgs = new CommandLineArgs(args);
            return BuildOptions(commandLineArgs);
        }

        /// <summary>
        /// Converts command line args into Options
        /// </summary>
        private static Options BuildOptions(CommandLineArgs commandLineArgs)
        {
            Options options = new Options();

            foreach (var arg in commandLineArgs)
            {
                switch (arg.Keyword)
                {
                    case Constants.ARG_LAUNCHSTATE:
                        LaunchStates launchState;
                        if (Enum.TryParse(arg.Value, out launchState))
                        {
                            options.LaunchState = launchState;
                        }
                        else
                        {
                            throw new ApplicationException("Invalid launch state: " + arg.Value);
                        }
                        break;

                    case Constants.ARG_BATCHARGS:
                        options.ArgumentList = arg.Value;
                        break;

                    case Constants.ARG_BATCHFILE:
                        options.BatchFile = arg.Value;
                        break;

                    case Constants.ARG_WORKINGDIR:
                        options.WorkingDir = arg.Value;
                        break;
                }
            }

            return options;
        }
    }
}
